﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace EshubaAnalyser
{
    public static class HttpUtil
    {
        private const string HttpMethodPost = "POST";
        private const string HttpMethodGet = "GET";
        private static readonly Encoding DefualtEncoding = new UTF8Encoding(false);
        private static readonly WebProxy Proxy =
            new WebProxy("proxy.huaweisymantec.com:8080") { Credentials = new NetworkCredential("l00003768", "Huasai2053") };

        public static WebResponse PostResponse(int timeOut, string url, IDictionary<string, object> paramList)
        {
            ServicePointManager.DefaultConnectionLimit = 20;
            HttpWebRequest request = null;
            WebResponse response = null;
            try
            {
                CookieContainer cc = new CookieContainer();
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = HttpMethodPost;
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = cc;
                request.Timeout = timeOut;
                //设置空代理，即不支持代理
                request.Proxy = Proxy;

                string data = ParseParamDictionary(paramList);
                request.ContentLength = Encoding.UTF8.GetByteCount(data);
                StreamWriter writer = new StreamWriter(request.GetRequestStream(), DefualtEncoding);
                writer.Write(data);
                writer.Flush();
                writer.Close();

                response = request.GetResponse();
                return response;
            }
            catch
            {
                if (request != null) request.Abort();
                if (response != null) response.Close();
                return null;
            }
        }

        public static WebResponse GetResponse(string url, IDictionary<string, object> paramList)
        {
            ServicePointManager.DefaultConnectionLimit = 20;
            try
            {
                //GET方式
                string data = ParseParamDictionary(paramList);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + data);
                request.Method = HttpMethodGet;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Proxy = Proxy;
                CookieContainer cc = new CookieContainer();
                request.CookieContainer = cc;
                return request.GetResponse();
            }
            catch
            {
                return null;
            }
        }

        private static string ParseParamDictionary(IDictionary<string, object> paramList)
        {
            StringBuilder data = new StringBuilder("");
            data.Append("t=").Append(new Random().Next()).Append("&");
            string requestData = "";
            if (paramList == null || paramList.Count == 0)
            {
                return requestData;
            }
            foreach (string key in paramList.Keys)
            {
                string escapeKey = Uri.EscapeDataString(key);
                string escapeValue = paramList[key] == null || string.IsNullOrEmpty(paramList[key].ToString()) ? string.Empty : Uri.EscapeDataString(paramList[key].ToString());
                if (paramList[key] is string[])
                {
                    escapeValue = string.Empty;
                    string[] arrayParam = paramList[key] as string[];
                    foreach (string item in arrayParam)
                    {
                        data.Append(escapeKey + "=" + item + "&");
                    }
                }
                data.Append(escapeKey + "=" + escapeValue + "&");
            }
            requestData = data.ToString();
            requestData = requestData.Substring(0, requestData.Length - 1);
            return requestData;
        }
    }
}
